`define PANDORA_REG_DELAY #10
module sim_mem_2p (clk, ce0, a0, do0, ce1, we1, a1, di1);

  parameter ADDR_WIDTH   =   8;
  parameter DEPTH        = 256;
  parameter DATA_WIDTH   =  32;
  parameter NAME         = "sim_mem_2p";


  input     clk;
  input     ce0;
  input  [ADDR_WIDTH-1:0]   a0;
  output [DATA_WIDTH-1:0]  do0;
  input                    ce1;
  input                    we1;
  input [ADDR_WIDTH-1:0]   a1;
  input [DATA_WIDTH-1:0]   di1;

  reg [DATA_WIDTH-1:0]          do0;
  reg [DATA_WIDTH-1:0]          data [DEPTH-1:0];


  always @(posedge clk) begin
    if ( ce0 ) begin 
      do0      <= `PANDORA_REG_DELAY data[a0];
    end
    else if ( !ce0 ) begin
      // not active; do nothing
    end
    else begin
      do0      <= `PANDORA_REG_DELAY {DATA_WIDTH{1'bx}};
    end
  end 



  always @(posedge clk) begin
    if ( ce1 ) begin
      if ( we1 ) begin
        data[a1] <= `PANDORA_REG_DELAY di1;
      end
      else if ( !we1 ) begin
      end
      else begin
        data[a1] <= `PANDORA_REG_DELAY {DATA_WIDTH{1'bx}};
      end
    end 
    else if ( !ce1 ) begin
      // not active; do nothing
    end
    else begin
      if ( !we1 ) begin
        // not writing
      end
      else begin
        // possibly writing
        data[a1] <= `PANDORA_REG_DELAY {DATA_WIDTH{1'bx}};
      end
    end
  end



  // Sanity checking
`ifdef DEBUG_MEM
  always @(posedge clk) begin
    if ( ce0 === 1'bx || ce0 === 1'bz )
      $display("WARNING: Memory %0s got unknown ce0 at time %t", NAME, $time);

    if ( ce1 === 1'bx || ce1 === 1'bz )
      $display("WARNING: Memory %0s got unknown ce1 at time %t", NAME, $time);
    if ( ce1 === 1'b1 && (we1 === 1'bx || we1 === 1'bz ) )
      $display("WARNING: Memory %0s got unknown we1 at time %t", NAME, $time);
    if ( ce1 === 1'b1 && we1 === 1'b1 && ( (^a1) === 1'bx ) )
      $display("WARNING: Memory %0s got unknown a1 while writing at time %t", NAME, $time);
    if ( ce0 === 1'b1 && a0 >= HEIGHT )
      $display("WARNING: Memory %0s accessing outside defined address range at time %t", NAME, $time);
  end
`endif


endmodule
